package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtils;
import com.example.lotterysystem.controller.param.DrawPrizeParam;
import com.example.lotterysystem.controller.param.ShowWinningRecordsParam;
import com.example.lotterysystem.controller.result.WinningRecordResult;
import com.example.lotterysystem.service.DrawPrizeService;
import com.example.lotterysystem.service.dto.WinningRecordResultDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeTiersEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class DrawPrizeController {

    @Autowired
    private DrawPrizeService drawPrizeService;
    private static final Logger logger = LoggerFactory.getLogger(DrawPrizeController.class);
    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(
            @Validated @RequestBody DrawPrizeParam param) {
        logger.info("drawPrize DrawPrizeParam{}",param);
        drawPrizeService.drawPrize(param);
        return CommonResult.success(true);
    }

    @RequestMapping("/winning-record/show")
    public CommonResult<List<WinningRecordResult>> showWinningRecords(
            @Validated @RequestBody ShowWinningRecordsParam param) {
        logger.info("showWinningRecords ShowWinningRecordsParam{}",
                JacksonUtils.writeValueString(param));
        List<WinningRecordResultDTO> winningRecordResultDTOList = drawPrizeService.getRecords(param);
        return CommonResult.success(convertToWinningRecordResult(winningRecordResultDTOList));
    }

    private List<WinningRecordResult> convertToWinningRecordResult(
            List<WinningRecordResultDTO> winningRecordResultDTOList) {
        if(CollectionUtils.isEmpty(winningRecordResultDTOList)) {
            logger.info("");
            return Arrays.asList();
        }
        return winningRecordResultDTOList
                .stream()
                .map(winningRecordResultDTO -> {
                    WinningRecordResult winningRecordResult = new WinningRecordResult();
                    winningRecordResult.setWinningId(winningRecordResultDTO.getWinningId());
                    winningRecordResult.setWinnerName(winningRecordResultDTO.getWinnerName());
                    winningRecordResult.setPrizeName(winningRecordResultDTO.getPrizeName());
                    winningRecordResult.setPrizeTier(winningRecordResultDTO.getPrizeTier().getMessage());
                    winningRecordResult.setWinningTime(winningRecordResultDTO.getWinningTime());
                    return winningRecordResult;
                }).collect(Collectors.toList());
    }

}
